//
//  NSOperationQueue_VC.m
//  DXC
//
//  Created by TOPTEAM on 2017/5/9.
//  Copyright © 2017年 TOPTEAM. All rights reserved.
//

#import "NSOperationQueue_VC.h"
#import "NSQueue_Operation.h"
@interface NSOperationQueue_VC ()

@end

@implementation NSOperationQueue_VC

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self blockOperation];
}

-(void)custom
{
    //1.创建队列
    
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    
    //2.封装操作
    NSQueue_Operation *op1 = [[NSQueue_Operation alloc]init];
    NSQueue_Operation *op2 = [[NSQueue_Operation alloc]init];
    
    //3.添加操作到队列
    [queue addOperation:op1];   //[op1 start]
    [queue addOperation:op2];
}
-(void)blockOperation
{
    //1.创建队列
    /*
     并发:全局并发队列,自己创建(concurrent)
     串行:主队列,自己创建(serial)
     */
    
    //    NSOperationQueue
    /*
     主队列:凡是放到主队列里面的任务都在主线程执行[NSOperationQueue mainQueue]
     非主队列:alloc int,同时具备了并发和串行的功能,默认是并发队列
     */
    
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    
    //2.封装操作
    NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"1----%@",[NSThread currentThread]);
    }];
    
    NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"2----%@",[NSThread currentThread]);
    }];
    
    NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"3----%@",[NSThread currentThread]);
    }];
    
    [op3 addExecutionBlock:^{
        NSLog(@"4----%@",[NSThread currentThread]);
    }];
    
    [op3 addExecutionBlock:^{
        NSLog(@"5----%@",[NSThread currentThread]);
    }];
    
    
    [op3 addExecutionBlock:^{
        NSLog(@"6----%@",[NSThread currentThread]);
    }];
    
    
    //3.添加操作到队列
    [queue addOperation:op1];   //[op1 start]
    [queue addOperation:op2];
    [queue addOperation:op3];
    
    //简便方法
    [queue addOperationWithBlock:^{
        NSLog(@"7----%@",[NSThread currentThread]);
    }];
    
    [queue addOperationWithBlock:^{
        NSLog(@"8----%@",[NSThread currentThread]);
    }];
    
}
-(void)invocation
{
    //1.创建队列
    /*
     并发:全局并发队列,自己创建(concurrent)
     串行:主队列,自己创建(serial)
     */
    
    //    NSOperationQueue
    /*
     主队列:凡是放到主队列里面的任务都在主线程执行[NSOperationQueue mainQueue]
     非主队列:alloc int,同时具备了并发和串行的功能,默认是并发队列
     */
    
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    
    //2.封装操作
    NSInvocationOperation *op1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download1) object:nil];
    
    NSInvocationOperation *op2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download2) object:nil];
    
    NSInvocationOperation *op3 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download3) object:nil];
    
    //3.添加操作到队列
    [queue addOperation:op1];   //[op1 start]
    [queue addOperation:op2];
    [queue addOperation:op3];
}

-(void)download1
{
    NSLog(@"download1---%@",[NSThread currentThread]);
}

-(void)download2
{
    NSLog(@"download2---%@",[NSThread currentThread]);
}

-(void)download3
{
    NSLog(@"download3---%@",[NSThread currentThread]);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    self.title=@"NSOperationQueue";
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
